package com.task.controller;

import com.github.pagehelper.PageInfo;
import com.task.common.ReturnResult;
import com.task.pojo.User;
import com.task.server.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户管理
 *
 * @author JiaFuJun
 * @version v1.0
 * @date 2020-01-04
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userservice;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ReturnResult save(HttpServletRequest request, HttpServletResponse response, User record) {
        if (record.getId()==null) {
            userservice.insert(record);
        } else {
            userservice.update(record);
        }
        return ReturnResult.ok();
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ReturnResult del(HttpServletRequest request, HttpServletResponse response, Integer id) {
        if (id == null) {
            return ReturnResult.build("id不能为空");
        }

        userservice.del(id);
        return ReturnResult.ok();
    }

    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.POST)
    public ReturnResult selectByPrimaryKey(HttpServletRequest request, HttpServletResponse response, Integer id) {
        if (id == null) {
            return ReturnResult.build("id不能为空");
        }

        User info = userservice.selectByPrimaryKey(id);
        return ReturnResult.ok(info);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ReturnResult getList(HttpServletRequest request, HttpServletResponse response, User record) {
        /**条件判断**/


        List<User> list = userservice.getList(record);
        return ReturnResult.ok(list);
    }

    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    public ReturnResult pageList(HttpServletRequest request, HttpServletResponse response, User record,
                                 @RequestParam(value="pageNo",defaultValue = "1") Integer pageNo,
                                 @RequestParam(value="pageSize",defaultValue = "20") Integer pageSize) {
        /**条件判断**/


        PageInfo<User> info = userservice.pageList(record,pageNo,pageSize);
        return ReturnResult.ok(info);
    }
}